package zsc.nine.onlinemall.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * @ClassName: SendMailTool
 * @Description: Tool of sending Mail
 * @Author: Zainus
 * @CreateDate: Jul.20, 2021
 * @UpdateDate: July.24, 2021
 * @Version: [v2.0]
 */

/**
 * @类名: SendMailTool
 * @描述: 发送邮件工具类
 * @创建人: 叶增钥
 * @创建时间: 2021年7月20日
 * @更新时间: 2021年7月24日
 * @版本: [v2.0]
 */
@Component
public class SendMailTool {

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * @param Captcha:     验证码
     * @param Type:        验证类型 0为注册 1为找回密码 2为更改绑定邮箱
     * @param MailAddress: 邮箱地址
     * @param UserName:    用户名
     * @Description: 发送邮箱验证码的方法
     * @return: 发送结果 true为成功 false为失败
     */
    public Boolean sendCatcha(String Captcha, String Type, String MailAddress, String UserName) {

        /**不同类型消息*/
        String TypeMessage;

        /**邮件信息*/
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

        /**设置发件人*/
        simpleMailMessage.setFrom("源浩线上商城" + " < " + "2741611042@qq.com" + " > ");

        /**设置收件人*/
        simpleMailMessage.setTo(MailAddress);

        /**设置邮件标题、主体、验证码*/
        simpleMailMessage.setSubject("验证码为: " + Captcha);
        simpleMailMessage.setText("亲爱的用户：【 " + UserName + " 】 \n");

        switch (Type) {

            case "0":
                TypeMessage = "欢迎注册！ \n";
                break;

            case "1":
                TypeMessage = "请尽快完成找回密码操作！ \n";
                break;

            case "2":
                TypeMessage = "您正在更改绑定邮箱。如果不是您本人操作，请不要将验证码透露给任何人！ \n";
                break;

            default:
                TypeMessage = "\n";
                break;

        }

        simpleMailMessage.setText(simpleMailMessage.getText()
                + TypeMessage
                + "您的验证码为： "
                + Captcha
                + "。 \n");

//        System.out.println(simpleMailMessage.getSubject());
//        System.out.println(simpleMailMessage.getText());
//        System.out.println("Mail is sent to: " + Arrays.toString(simpleMailMessage.getTo()));

        /**用JavaMailSender类尝试发送*/
        try {

            javaMailSender.send(simpleMailMessage);
            return true;

        } catch (Exception e) {

            e.printStackTrace();
            return false;

        }

    }


    /**
     * @param Type:        类型 0为发货 1为退款
     * @param MailAddress: 邮箱地址
     * @param UserName:    用户名
     * @Description: 发送服务邮件的工具类方法
     * @return: 发送结果 true为成功 false为失败
     */
    public Boolean sendMail(String Type, String MailAddress, String UserName) {

        /**不同类型消息*/
        String TypeMessage;

        /**邮件信息*/
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();

        /**设置发件人*/
        simpleMailMessage.setFrom("源浩线上商城" + " < " + "2741611042@qq.com" + " > ");

        /**设置收件人*/
        simpleMailMessage.setTo(MailAddress);

        /**设置邮件标题、主体、验证码*/
        simpleMailMessage.setText("亲爱的用户：【 " + UserName + " 】 \n");

        switch (Type) {

            case "0":
                simpleMailMessage.setSubject("订单已发货");
                TypeMessage = "您的订单已发货！ \n";
                break;

            case "1":
                simpleMailMessage.setSubject("退款申请已通过");
                TypeMessage = "您的退款申请已通过！ \n";
                break;

            default:
                TypeMessage = "\n";
                break;

        }

        simpleMailMessage.setText(simpleMailMessage.getText()
                + TypeMessage);

        /**用JavaMailSender类尝试发送*/
        try {

            javaMailSender.send(simpleMailMessage);
            return true;

        } catch (Exception e) {

            e.printStackTrace();
            return false;

        }

    }

}
